<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="68px"
    >
      <el-form-item label="报销日期" prop="billDate">
        <el-date-picker
            v-model="queryParams.billDate"
            value-format="YYYY-MM-DD"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="费用类型" prop="expenseType">
        <el-select
            v-model="queryParams.expenseType"
            placeholder="请选择费用类型"
            clearable
            class="!w-240px"
        >
          <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.OA_EXPENSE_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="请选择审批状态"
            clearable
            class="!w-240px"
        >
          <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <!-- <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['oa:expense:dept-export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="报销编号" align="center" prop="billNo" min-width="100" />
      <el-table-column label="报销人" align="center" prop="userName" min-width="100" />
      <el-table-column label="费用归属部门" align="center" prop="deptName" min-width="110" />
      <el-table-column label="报销公司" align="center" prop="subjectName" min-width="350" />
      <el-table-column label="费用类型" align="center" prop="expenseType" min-width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_EXPENSE_TYPE" :value="scope.row.expenseType" />
        </template>
      </el-table-column>
      <el-table-column label="费用名称" align="center" prop="expenseName" min-width="80">
        <template #default="scope">
          <dict-tag :type="scope.row.expenseType" :value="scope.row.expenseName" />
        </template>
      </el-table-column>
      <el-table-column label="费用发生日期" align="center" prop="billDate" :formatter="dateFormatter2" min-width="110" />
      <el-table-column label="报销金额" align="center" prop="expenseAmount" min-width="80" />
      <el-table-column label="有无发票" align="center" prop="hasInvoice" min-width="90">
        <template #default="scope">
          <el-tag :type="scope.row.hasInvoice === 1 ? 'success' : 'danger'">{{ scope.row.hasInvoice === 1 ? '有' : '无' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" prop="status" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus" min-width="120">
        <template #default="scope">
          <div class="flex items-center justify-center gap-2">
            <span v-if="scope.row.payStatus === 1">已支付</span>
            <span v-else-if="scope.row.payStatus === 2">已取消</span>
            <span v-else>未支付</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="付款日期" align="center" prop="payDate" :formatter="dateFormatter2" min-width="110" />
      <el-table-column label="付款人" align="center" prop="payerName" min-width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" min-width="180" />
      <el-table-column label="操作" align="center" min-width="120" fixed="right">
        <template #default="scope">
          <el-button
              link
              type="primary"
              @click="handleDetail(scope.row)"
              v-hasPermi="['oa:expense:query']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>

  <!-- 详情抽屉 -->
  <DetailDrawer
    v-model="showDetailDrawer"
    :task-id="currentExpenseId"
    title="报销详情" />
</template>

<script setup lang="ts">
import { ExpenseApi, ExpenseVO } from '@/api/oa/expense'
import DetailDrawer from './components/DetailDrawer.vue'
import download from '@/utils/download'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'

/** 本部门费用报销申请 列表 */
defineOptions({ name: 'ExpenseDeptIndex' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ExpenseVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  billDate: undefined,
  expenseType: undefined,
  status: undefined,
})
const exportLoading = ref(false) // 导出的加载中
const queryFormRef = ref() // 搜索的表单

// 详情抽屉相关
const showDetailDrawer = ref(false)
const currentExpenseId = ref<number>()

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 详情操作 */
const handleDetail = (row: ExpenseVO) => {
  currentExpenseId.value = Number(row.id)
  showDetailDrawer.value = true
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const params = {
      ...queryParams,
    }
    const data = await ExpenseApi.exportDeptExpense(params)
    download.excel(data, '本部门费用报销申请.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 获得列表 */
const getList = async () => {
  loading.value = true
  try {
    const params = {
      ...queryParams,
    }
    const data = await ExpenseApi.getDeptExpensePage(params)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 格式化金额显示 */
const formatMoney = (amount: number) => {
  if (!amount) return '0.00'
  return Number(amount).toFixed(2)
}

onMounted(() => {
  getList()
})
</script> 